﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASPItalia.ModelVirtualCasting;
using ASPItalia.ModelVirtualCasting.WCF.Contracts;
using ObjectModel = ASPItalia.ModelVirtualCasting.ObjectModel;
using System.ServiceModel.Activation;
using System.Threading;
using System.Collections.ObjectModel;

namespace ASPItalia.ModelVirtualCasting.WCF.Services
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ModelsManager : IModelsManager
    {
        #region IModelsManager Members

        public GetAllCandidatesResponse GetAllCandidates(GetAllCandidatesRequest request)
        {
            GetAllCandidatesResponse response = new GetAllCandidatesResponse();
            response.Models = (from m in ApplicationContext.Current.Candidates.Include("Multimedia").GetAll().ToArray()
                               select GetModel(m)).ToArray();

            return response;
        }

        public GetAllModelsResponse GetAllModels(GetAllModelsRequest request)
        {
            //Thread.Sleep(3000);

            GetAllModelsResponse response = new GetAllModelsResponse();
            //response.Models = (from m in ApplicationContext.Current.Models.Include("Multimedia").GetAll().ToArray()
            //                   select GetModel(m)).ToArray();

            //response.Models = new Model[]
            //      {
            //          new Model { FirstName = "Anna", LastName = "Magnani",
            //              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },
            //          new Model { FirstName = "Anna", LastName = "Signorini",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "13616.jpg"},
            //                  new Image { FileName = "12330.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          }, 
            //          new Model { FirstName = "Antonella", LastName = "Signorini",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "13616.jpg"},
            //                  new Image { FileName = "12330.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },
            //          new Model { FirstName = "Antonella", LastName = "Young",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },new Model { FirstName = "Martina", LastName = "Astro",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },new Model { FirstName = "Fidel", LastName = "Astro",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },new Model { FirstName = "Michel", LastName = "Martini",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },new Model { FirstName = "Pamela", LastName = "Young",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },
            //      };
            return response;
        }

        private static Model GetModel(ObjectModel.Candidate m)
        {
            return new Model
            {
                FirstName = m.FirstName,
                LastName = m.LastName,
                HairColor = m.PhysicalProfile.HairColor,
                EyeColor = m.PhysicalProfile.EyesColor,
                ResidenceCity = m.ResidenceCity
              ,
                Multimedia = (from mul in m.Multimedia
                              select (mul is ObjectModel.Video) ? (Multimedia)new Video { FileName = mul.FileName } : new Image { FileName = mul.FileName }).ToArray()
            };
        }

        #endregion

        #region IModelsManager sample

        public GetAllModelsResponse GetAllModelsOld(GetAllModelsRequest request)
        {
            GetAllModelsResponse response = new GetAllModelsResponse();
            response.Models = new Model[]
            {
                new Model { FirstName = "Anna", LastName = "Magnani",
                    Multimedia = new Multimedia[] {
                        new Image { FileName = "12330.jpg"},
                        new Image { FileName = "13616.jpg"},
                        new Video { FileName = "test.wmv"},
                    },
                },
                new Model { FirstName = "Anna", LastName = "Signorini",
                                    Multimedia = new Multimedia[] {
                        new Image { FileName = "13616.jpg"},
                        new Image { FileName = "12330.jpg"},
                        new Video { FileName = "test.wmv"},
                    },
                }, 
                new Model { FirstName = "Antonella", LastName = "Signorini",
                                    Multimedia = new Multimedia[] {
                        new Image { FileName = "13616.jpg"},
                        new Image { FileName = "12330.jpg"},
                        new Video { FileName = "test.wmv"},
                    },
                },
                new Model { FirstName = "Antonella", LastName = "Young",
                                    Multimedia = new Multimedia[] {
                        new Image { FileName = "12330.jpg"},
                        new Image { FileName = "13616.jpg"},
                        new Video { FileName = "test.wmv"},
                    },
                },
            };

            return response;
        }

        #endregion
    }
}
